using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using UnityEngine;

public abstract class HOConfigurationReader : MonoBehaviour
{
	public virtual Hashtable ParseData(TextAsset info, Hashtable table)
	{
		XmlDocument xmlDocument = new XmlDocument();
		XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
		xmlNamespaceManager.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
		xmlNamespaceManager.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
		xmlNamespaceManager.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
		xmlDocument.LoadXml(info.text);
		XmlNodeList xmlNodeList = xmlDocument.DocumentElement.SelectNodes("//ss:Worksheet/ss:Table/ss:Row", xmlNamespaceManager);
		XmlNodeList xmlNodeList2 = xmlNodeList[0].SelectNodes("ss:Cell", xmlNamespaceManager);
		string[] array = new string[xmlNodeList2.Count];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = xmlNodeList2[i].SelectSingleNode("ss:Data", xmlNamespaceManager).InnerText.Trim();
		}
		for (int j = 2; j < xmlNodeList.Count; j++)
		{
			XmlNodeList xmlNodeList3 = xmlNodeList[j].SelectNodes("ss:Cell", xmlNamespaceManager);
			Hashtable hashtable = new Hashtable();
			int num = 0;
			for (int k = 0; k < array.Length; k++)
			{
				if (num >= xmlNodeList3.Count)
				{
					for (int l = k; l < array.Length; l++)
					{
						ParseRepeatKey(hashtable, array[l], "0");
					}
					break;
				}
				if (xmlNodeList3[num].Attributes["ss:Index"] != null)
				{
					int num2 = k;
					k = int.Parse(xmlNodeList3[num].Attributes["ss:Index"].Value) - 1;
					if (k >= array.Length)
					{
						for (int m = num2; m < array.Length; m++)
						{
							ParseRepeatKey(hashtable, array[m], "0");
						}
						UnityEngine.Debug.LogWarning("--Toto-- HOConfigurationReader->ParseData: Cell index overstep.");
						break;
					}
					for (int n = num2; n < k; n++)
					{
						ParseRepeatKey(hashtable, array[n], "0");
					}
				}
				XmlNode xmlNode = xmlNodeList3[num].SelectSingleNode("ss:Data", xmlNamespaceManager);
				string attName = array[k];
				string empty = string.Empty;
				empty = ((xmlNode == null || !(xmlNode.InnerText != string.Empty)) ? "0" : xmlNode.InnerText);
				ParseRepeatKey(hashtable, attName, empty);
				num++;
			}
			ProcessData(table, hashtable);
		}
		return table;
	}

	protected virtual void ProcessData(Hashtable bigTable, Hashtable subTable)
	{
		if ((string)subTable["ID"] == "0")
		{
			UnityEngine.Debug.LogWarning("--Toto-- HOConfigurationReader->ProcessData: subTable id is 0.");
		}
		else
		{
			bigTable.Add(subTable["ID"], subTable);
		}
	}

	protected static string ParseTableValueToString(object val)
	{
		string text = (string)val;
		if (text == "0")
		{
			return string.Empty;
		}
		return text;
	}

	protected static int ParseTableValueToInt(object val)
	{
		return int.Parse((string)val);
	}

	protected static float ParseTableValueToFloat(object val)
	{
		return float.Parse((string)val);
	}

	protected static bool ParseTableValueToBoolean(object val)
	{
		return Convert.ToBoolean(ParseTableValueToInt(val));
	}

	protected void ParseRepeatKey(Hashtable table, string attName, string attValue)
	{
		if (table.ContainsKey(attName))
		{
			attValue = table[attName] + "&&" + attValue;
		}
		table[attName] = attValue;
	}

	public static void SerializeTable(object obj, string binaryAssetFileName)
	{
		string path = Application.dataPath + "/Data/Bin/" + binaryAssetFileName + ".bytes";
		using (FileStream serializationStream = new FileStream(path, FileMode.Create))
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			try
			{
				binaryFormatter.Serialize(serializationStream, obj);
			}
			catch (NotImplementedException ex)
			{
				UnityEngine.Debug.LogError("serialize exception : message = " + ex.Message + ", tostring = " + ex.ToString());
			}
		}
	}

	public static T DeSerializeTable<T>(TextAsset binarayAssetTable) where T : new()
	{
		T val = new T();
		using (MemoryStream serializationStream = new MemoryStream(binarayAssetTable.bytes))
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			return (T)binaryFormatter.Deserialize(serializationStream);
		}
	}
}
